package com.example.adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import com.example.beans.PaperAdavice;
import com.example.beans.PaperAdvice1;
import com.example.inter.AdapterCallback;
import com.example.myapplication.AdviceActivity;
import com.example.myapplication.R;
import com.github.siyamed.shapeimageview.CircularImageView;
import com.lidroid.xutils.BitmapUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.zip.Inflater;

/**
 * Created by Administrator on 2016/3/28.
 */
public class PaperAdviceAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private Context context;
    private LayoutInflater inflater;
    private List<PaperAdavice> list = new ArrayList<>();
    private BitmapUtils bitmapUtils;
    public LinearLayoutManager layoutManager;
    public PaperAdviceAdapter1 paperAdviceAdapter1;
    public AdapterCallback adapterCallback;

    public View view ;

    public PaperAdviceAdapter(Context context, List<PaperAdavice> list, AdapterCallback adapterCallback) {
        inflater = LayoutInflater.from(context);
        this.context = context;
        this.list = list;
        this.adapterCallback = adapterCallback;

    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
         view = inflater.inflate(R.layout.item_advice_first, parent, false);
        view.setLayoutParams(new RecyclerView.LayoutParams(RecyclerView.LayoutParams.MATCH_PARENT, RecyclerView.LayoutParams.WRAP_CONTENT));
        MyViewHolder myViewHolder = new MyViewHolder(view);
        bitmapUtils = new BitmapUtils(context);
        return myViewHolder;
    }


    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int position) {

        layoutManager = new LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false);
        ((MyViewHolder) holder).recyclerView.setLayoutManager(layoutManager);
        ((MyViewHolder) holder).recyclerView.addItemDecoration(new DividerItemDecoration(context,
                DividerItemDecoration.VERTICAL_LIST));
        final boolean b = list.get(position).getTAG();
        if (b == true) {
            ((MyViewHolder) holder).user_name.setText(list.get(position).getStudent_name());
            ((MyViewHolder) holder).tv_content.setText(list.get(position).getAdvice_commit());
            int p = holder.getAdapterPosition();
            List<PaperAdvice1> mlist = list.get(p).getList();

            if (mlist != null && mlist.get(0).getAdvice_id() != null) {
                PaperAdviceAdapter1 paperAdviceAdapter1 = new PaperAdviceAdapter1(context, mlist);
                ((MyViewHolder) holder).recyclerView.setAdapter(paperAdviceAdapter1);
                paperAdviceAdapter1.notifyDataSetChanged();
                ((MyViewHolder) holder).recyclerView.setVisibility(View.VISIBLE);
            } else {
                ((MyViewHolder) holder).recyclerView.setVisibility(View.GONE);
            }

            String url = list.get(position).getStudent_pic();
            if (url.charAt(0) == '.') {
                url = url.substring(1);
            }
            bitmapUtils.display(((MyViewHolder) holder).circularImageView, "http://192.168.191.1/app/pic" + url);
            ((MyViewHolder) holder).btn_reply.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    String paperId = list.get(position).getPaper_id();
                    String adviceId = list.get(position).getAdvice_id();
                    Intent intent = new Intent();
                    intent.setClass(context, AdviceActivity.class);
                    Bundle bundle = new Bundle();
                    adapterCallback.callBack(position,1);
                    bundle.putString("paperId", paperId);
                    bundle.putString("adviceId", adviceId);
                    intent.putExtras(bundle);
                    context.startActivity(intent);

                }
            });
        } else if (b == false) {
            ((MyViewHolder) holder).user_name.setText(list.get(position).getTeacher_name());
            ((MyViewHolder) holder).tv_content.setText(list.get(position).getAdvice_commit());

            List<PaperAdvice1> mlist = list.get(position).getList();
            if (mlist != null && mlist.get(0).getAdvice_id() != null) {
                PaperAdviceAdapter1 paperAdviceAdapter1 = new PaperAdviceAdapter1(context, mlist);
                ((MyViewHolder) holder).recyclerView.setAdapter(paperAdviceAdapter1);
                paperAdviceAdapter1.notifyDataSetChanged();
                ((MyViewHolder) holder).recyclerView.setVisibility(View.VISIBLE);
            } else {
                ((MyViewHolder) holder).recyclerView.setVisibility(View.GONE);
            }
            String url = list.get(position).getTeacher_pic();
            if (url.charAt(0) == '.') {
                url = url.substring(1);
            }
            bitmapUtils.display(((MyViewHolder) holder).circularImageView, "http://192.168.191.1/app/pic" + url);

        }
        ((MyViewHolder) holder).time.setText(list.get(position).getAdvice_date());

        ((MyViewHolder) holder).btn_reply.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String paperId = list.get(position).getPaper_id();
                String adviceId = list.get(position).getAdvice_id();
                Intent intent = new Intent();
                intent.setClass(context, AdviceActivity.class);
                Bundle bundle = new Bundle();
                adapterCallback.callBack(position,1);
                bundle.putString("paperId", paperId);
                bundle.putString("adviceId", adviceId);
                bundle.putInt("position",position);
                bundle.putString("tag","1");
                intent.putExtras(bundle);
                context.startActivity(intent);

            }
        });
    }
    public void update(int postion,MyViewHolder viewHolder){

        PaperAdviceAdapter1 paperAdviceAdapter1 = new PaperAdviceAdapter1(context,list.get(postion).getList());
        layoutManager = new LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false);
        viewHolder.recyclerView.setAdapter(paperAdviceAdapter1);
        paperAdviceAdapter1.notifyDataSetChanged();
        viewHolder.recyclerView.setVisibility(View.VISIBLE);
        notifyItemChanged(postion);
    }


    @Override
    public int getItemCount() {
        return list.size();
    }




    public class MyViewHolder extends RecyclerView.ViewHolder {
        public CircularImageView circularImageView;
        public TextView user_name, time, tv_content;
        public Button btn_reply;
        public RecyclerView recyclerView;

        public MyViewHolder(View itemView) {
            super(itemView);
            circularImageView = (CircularImageView) itemView.findViewById(R.id.circularImageView);
            user_name = (TextView) itemView.findViewById(R.id.user_name);
            time = (TextView) itemView.findViewById(R.id.time);
            btn_reply = (Button) itemView.findViewById(R.id.btn_reply);
            recyclerView = (RecyclerView) itemView.findViewById(R.id.recyclerView);
            tv_content = (TextView) itemView.findViewById(R.id.text_content);

        }
    }
}
